namespace CISLib
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class ManageProject
    {
        public static DataSet GetProjectInfo(Guid companyId)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            string strSQL = "Select Tb_ProjectInfo.*,TB_CompanyInfo.CompanyName from Tb_ProjectInfo,TB_CompanyInfo where TB_CompanyInfo.CompanyId = Tb_ProjectInfo.CompanyId and Tb_ProjectInfo.CompanyId=@CompanyId";
            MyDB.DBSetSelect(strSQL);
            return MyDB.DBReturnDataSet();
        }

        public static Project GetProjectInfoViaId(Guid projectId)
        {
            SqlDataReader MyReader = null;
            Project project = new Project();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "select * from Tb_ProjectInfo where ProjectId = @ProjectId";
            MyDB.AddParameters(projectId, "ProjectId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            if (MyReader.Read())
            {
                project.ProjectId = MyReader.GetGuid(0);
                project.CompanyId = MyReader.GetGuid(1);
                project.ProjectName = MyReader.GetString(2);
                project.TotalAmount = Convert.ToSingle(MyReader[3]);
                project.SignedDate = MyReader.GetDateTime(4);
                project.EffectDate = MyReader.GetDateTime(5);
                project.DifficultLevel = MyReader.GetString(6);
                project.ResponsiblePerson = MyReader.GetString(7);
                project.Other = MyReader.GetString(8);
            }
            MyDB.DBClose();
            return project;
        }

        public static void InsertNewProjectInfo(Guid companyId, string projetName, float totalAmount, DateTime signedDate, DateTime effectDate, string difficultLevel, string responsiblePerson, string other)
        {
            Guid id = Guid.NewGuid();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "ProjectId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(projetName, "ProjetName", SqlDbType.VarChar);
            MyDB.AddParameters(totalAmount, "TotalAmount", SqlDbType.Float);
            MyDB.AddParameters(signedDate, "SignedDate", SqlDbType.DateTime);
            MyDB.AddParameters(effectDate, "EffectDate", SqlDbType.DateTime);
            MyDB.AddParameters(difficultLevel, "DifficultLevel", SqlDbType.VarChar);
            MyDB.AddParameters(responsiblePerson, "ResponsiblePerson", SqlDbType.VarChar);
            MyDB.AddParameters(other, "Other", SqlDbType.VarChar);
            string strSQL = "Insert into Tb_ProjectInfo (ProjectId,CompanyId,ProjetName,TotalAmount,SignedDate,EffectDate,DifficultLevel,ResponsiblePerson,Other) values (@ProjectId,@CompanyId,@ProjetName,@TotalAmount,@SignedDate,@EffectDate,@DifficultLevel,@ResponsiblePerson,@Other)";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static bool IsExistSameProject(string name)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(name, "ProjetName", SqlDbType.VarChar);
            string strSQL = "select * from Tb_ProjectInfo where ProjetName = @ProjetName";
            MyDB.DBSetSelect(strSQL);
            if (MyDB.DBExecuteScalar() == null)
            {
                MyDB.DBClose();
                return false;
            }
            MyDB.DBClose();
            return true;
        }

        public static void RemoveProjectViaId(Guid projectId)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(projectId, "ProjectId", SqlDbType.UniqueIdentifier);
            string strSQL = "Delete From Tb_ProjectInfo where ProjectId = @ProjectId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateProjectInfo(Guid projectId, Guid companyId, string projetName, float totalAmount, DateTime signedDate, DateTime effectDate, string difficultLevel, string responsiblePerson, string other)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(projectId, "ProjectId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(projetName, "ProjetName", SqlDbType.VarChar);
            MyDB.AddParameters(totalAmount, "TotalAmount", SqlDbType.Float);
            MyDB.AddParameters(signedDate, "SignedDate", SqlDbType.DateTime);
            MyDB.AddParameters(effectDate, "EffectDate", SqlDbType.DateTime);
            MyDB.AddParameters(difficultLevel, "DifficultLevel", SqlDbType.VarChar);
            MyDB.AddParameters(responsiblePerson, "ResponsiblePerson", SqlDbType.VarChar);
            MyDB.AddParameters(other, "Other", SqlDbType.VarChar);
            string strSQL = "Update Tb_ProjectInfo set CompanyId = @CompanyId,ProjetName = @ProjetName,TotalAmount = @TotalAmount,SignedDate = @SignedDate,EffectDate = @EffectDate,DifficultLevel = @DifficultLevel,ResponsiblePerson=@ResponsiblePerson,other=@other where ProjectId = @ProjectId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }
    }
}

